package kit.docker.sms;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.Base64Kit;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.PropKit;

import java.util.HashMap;
import java.util.Map;

public class SMSKit {

    /**
     * 发送单条短信
     *
     * @param mobile  手机号码
     * @param message 短信内容(需包含签名信息)
     * @return SMSResult
     */
    public static SMSResult send(String mobile, String message, String sign) {
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Authorization", "Basic " + Base64Kit.encode(PropKit.get("lsm.sms.key")));
        String res = HttpKit.post(PropKit.get("lsm.sms.send"), null, "mobile=" + mobile + "&message=" + message + "【" + sign + "】", headerMap);
        return JSON.parseObject(res, SMSResult.class);
    }

    public static SMSResult send(String mobile, String message) {
        return send(mobile, message, PropKit.get("lsm.sms.sign"));
    }


    // =========================短信余额查询=============================

    /**
     * 查询账户余额
     *
     * @return jsonStr
     */
    private static int getBalance() {
        Map<String, String> headerMap = new HashMap<>();
        //headerMap.put("Authorization", "Basic " + new BASE64Encoder().encode(SMSConfig.API_KEY.getBytes("utf-8")));
        //headerMap.put("Authorization", "Basic " + SMSConfig.API_BASE64_KEY);
        headerMap.put("Authorization", "Basic " + PropKit.get("lsm.sms.key"));
        String res = HttpKit.post(PropKit.get("lsm.sms.status"), null, null, headerMap);
        JSONObject jo = JSON.parseObject(res);
        if (jo.getInteger("error") != 0) {
            throw new RuntimeException("短信API验证信息失败，error：" + jo.getInteger("error"));
        }
        return jo.getInteger("deposit");
    }

}
